package dr.server.impl.announce;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import dr.common.ResultModel;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AnnounceService {

    public ResultModel publish(String msgType, String title, String to, String messageOrTemplate, String url) {
        try {
            return addAnnounce( msgType,  title,  to,  messageOrTemplate, url);
        }catch (Exception e){
            return ResultModel.fail(e.getLocalizedMessage());
        }
    }

    public ResultModel addAnnounce(String msgType, String title, String to, String messageOrTemplate, String url) {
        Record drRecord = new Record();
        drRecord.set("member_id", to);
        drRecord.set("title", title);
        drRecord.set("content", messageOrTemplate);
        drRecord.set("type", msgType);
        drRecord.set("publish_date", new Date());
        drRecord.set("status", 1);
        drRecord.set("insert_time", new Date());
        drRecord.set("url", url);
        Db.save("dr_announce", drRecord);
        return ResultModel.ok();
    }
}
